1
|
|
|
define(function () { |
2
|
|
|
'use strict'; |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
reimplate after node-deb-version-compare under MIT |
6
|
|
|
(https://github.com/sdumetz/node-deb-version-compare) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
function Version(v) { |
10
|
|
|
var version = /^[a-zA-Z]?([0-9]*(?=:))?:(.*)/.exec(v); |
11
|
|
|
this.epoch = (version) ? version[1] : 0; |
12
|
|
|
version = (version && version[2]) ? version[2] : v; |
13
|
|
|
version = version.split('-'); |
14
|
|
|
this.debian = (version.length > 1) ? version.pop() : ''; |
15
|
|
|
this.upstream = version.join('-'); |
16
|
|
|
} |
17
|
|
|
Version.prototype.compare = function (b) { |
18
|
|
|
if ((this.epoch > 0 || b.epoch > 0) && Math.sign(this.epoch - b.epoch) !== 0) { |
19
|
|
|
return Math.sign(this.epoch - b.epoch); |
20
|
|
|
} |
21
|
|
|
if (this.compareStrings(this.upstream, b.upstream) !== 0) { |
22
|
|
|
return this.compareStrings(this.upstream, b.upstream); |
23
|
|
|
} |
24
|
|
|
return this.compareStrings(this.debian, b.debian); |
25
|
|
|
}; |
26
|
|
|
Version.prototype.charCode = function (c) { // the lower the charcode the lower the version. |
27
|
|
|
// if (c === '~') {return 0;} // tilde sort before anything |
28
|
|
|
// else |
29
|
|
|
if (/[a-zA-Z]/.test(c)) {return c.charCodeAt(0) - 'A'.charCodeAt(0) + 1;} else if (/[.:+-:]/.test(c)) {return c.charCodeAt(0) + 'z'.charCodeAt(0) + 1;} // charcodes are 46..58 |
30
|
|
|
return 0; |
31
|
|
|
}; |
32
|
|
|
|
33
|
|
|
// find index of "val" in "ar". |
34
|
|
|
Version.prototype.findIndex = function (ar, fn) { |
35
|
|
|
for (var i = 0; i < ar.length; i++) { |
36
|
|
|
if (fn(ar[i], i)) { |
37
|
|
|
return i; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
return -1; |
41
|
|
|
}; |
42
|
|
|
|
43
|
|
|
Version.prototype.compareChunk = function (a, b) { |
44
|
|
|
var ca = a.split(''); |
45
|
|
|
var cb = b.split(''); |
46
|
|
|
var diff = this.findIndex(ca, function (c, index) { |
47
|
|
|
if (cb[index] && c === cb[index]) return false; |
|
|
|
|
48
|
|
|
return true; |
49
|
|
|
}); |
50
|
|
|
if (diff === -1) { |
51
|
|
|
if (cb.length > ca.length) { |
52
|
|
|
if (cb[ca.length] === '~') { |
53
|
|
|
return 1; |
54
|
|
|
} |
55
|
|
|
return -1; |
56
|
|
|
} |
57
|
|
|
return 0; // no diff found and same length |
58
|
|
|
} else if (!cb[diff]) { |
59
|
|
|
return (ca[diff] === '~') ? -1 : 1; |
60
|
|
|
} |
61
|
|
|
return (this.charCode(ca[diff]) > this.charCode(cb[diff])) ? 1 : -1; |
62
|
|
|
}; |
63
|
|
|
|
64
|
|
|
Version.prototype.compareStrings = function (a, b) { |
65
|
|
|
if (a === b) return 0; |
|
|
|
|
66
|
|
|
var parseA = /([^0-9]+|[0-9]+)/g; |
67
|
|
|
var parseB = /([^0-9]+|[0-9]+)/g; |
68
|
|
|
var ra = parseA.exec(a); |
69
|
|
|
var rb = parseB.exec(b); |
70
|
|
|
while (ra !== null && rb !== null ) { |
71
|
|
|
if ((isNaN(ra[1]) || isNaN(rb[1])) && ra[1] !== rb[1] ) { // a or b is not a number and they're not equal. Note : "" IS a number so both null is impossible |
72
|
|
|
return this.compareChunk(ra[1], rb[1]); |
73
|
|
|
} // both are numbers |
74
|
|
|
if (ra[1] !== rb[1]) { |
75
|
|
|
return (parseInt(ra[1], 10) > parseInt(rb[1], 10)) ? 1 : -1; |
76
|
|
|
} |
77
|
|
|
ra = parseA.exec(a); |
78
|
|
|
rb = parseB.exec(b); |
79
|
|
|
} |
80
|
|
|
if (!ra && rb) { // rb doesn't get exec-ed when ra == null |
81
|
|
|
return (parseB.exec(b)[1].split('')[0] === '~') ? 1 : -1; |
82
|
|
|
} else if (ra && !rb) { |
83
|
|
|
return (ra[1].split('')[0] === '~') ? -1 : 1; |
84
|
|
|
} |
85
|
|
|
return 0; |
86
|
|
|
}; |
87
|
|
|
return function compare(a, b) { |
88
|
|
|
var va = new Version(a[0]); |
89
|
|
|
var vb = new Version(b[0]); |
90
|
|
|
return vb.compare(va); |
91
|
|
|
}; |
92
|
|
|
}); |
93
|
|
|
|
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.